﻿using HCore;

namespace MEat
{
    public class Spring : MoveEntity
    {
        public Vector3 anchorPosition;
        public Fix restLength;
        public Fix k;

        public Spring(int eType, HGame game) : base(eType, game)
        {
        }

        public override void Update(Fix deltaTime)
        {
            Vector3 gravity = new Vector3(0, -10, 0);
            Vector3 dir = position - anchorPosition;
            Fix l = dir.magnitude;
            Fix f = k * (l - restLength);
            Vector3 force = new Vector3(0, f, 0);
            Vector3 accelerate = force + gravity;
            velocity += accelerate * deltaTime;
            position += velocity * deltaTime;

            base.Update(deltaTime);
        }

        public override void Dispose()
        {
        }

        public override int CalculateChecksum()
        {
            return position.x.Raw ^ position.y.Raw ^ position.z.Raw;
        }
    }
}